import 'dart:async';
import 'dart:isolate';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';
import 'package:sj001/drawerHome.dart';
import 'package:sj001/pages/login/loginPage.dart';
import 'base/appRouteObserver.dart';
import 'base/networkProvider.dart';

void main() {
  Isolate.current.addErrorListener(
    RawReceivePort((dynamic pair) async {
      final isolateError = pair as List<dynamic>;
      print('catch exception isolateError: ${isolateError.toString()}');
      showToast("catch exception isolateError: ${isolateError.toString()}",duration: const Duration(seconds: 15));

      // customerReport(details);
    }).sendPort,
  );

  FlutterError.onError = (FlutterErrorDetails details) {
    print(
        "catch exception:${details.exception.toString()},\n  stack:${details.stack?.toString()}");
    if (details.stack != null) {
      Zone.current
          .handleUncaughtError(details.exception, details.stack!); //Tag1
    }
    showToast("catch exception:${details.exception.toString()},\n  stack:${details.stack?.toString()}",duration: const Duration(seconds: 15));

  };

  runZonedGuarded(
    () async {
      WidgetsFlutterBinding.ensureInitialized();

      runApp(
        MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (_) => NetworkProvider()),
          ],
          child: OKToast(
            child: GetMaterialApp(
              scrollBehavior: const MaterialScrollBehavior()
                  .copyWith(scrollbars: true, dragDevices: <PointerDeviceKind>{
                PointerDeviceKind.touch,
                PointerDeviceKind.mouse,
                PointerDeviceKind.stylus,
                PointerDeviceKind.invertedStylus,
                PointerDeviceKind.unknown
              }),
              home: const LoginPage(),
              onInit: () {},
              navigatorObservers: [AppRouteObserver().routeObserver],
              defaultTransition: Transition.rightToLeft,
              theme: ThemeData(
                primarySwatch: Colors.blue,
              ),
              builder: (context, child) {
                final easyloading = EasyLoading.init();
                child = easyloading(context, child);
                return child;
              },
            ),
          ),
        ),
      );
    },
    (error, stack) {
      print(
          "catch exception: ${error.toString()}\n  Stack：${stack.toString()}");
      showToast("catch exception: ${error.toString()}\n  Stack：${stack.toString()}",duration: const Duration(seconds: 15));
    },
  );
}

